/*
 * Draw layouts * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 */

/*
 * Draw layout for the Sequence view
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawSequenceViewStructure = function(selectedChromosome, selectedSequenceStart, selectedSequenceEnd, featureSequence){
	var sequenceLength = selectedSequenceEnd - (selectedSequenceStart-1);
	if(featureSequence){
		var titleName = selectedChromosome;
		var titleInformation = ', Start: ' + selectedSequenceStart + ', End: ' + selectedSequenceEnd + ', Length: ' + sequenceLength;
		var featureNumber = document.createElement("span");
	} else {
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.sequenceConf.coordinates.chromosomeId = selectedChromosome;
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.sequenceConf.coordinates.start = selectedSequenceStart;
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.sequenceConf.coordinates.end = selectedSequenceEnd;
		
		var chrIdName = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].id;
		var chrIdType = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].type;
		
		var titleName = ' ' + chrIdType.toUpperCase() + ' ' + chrIdName;
		var titleInformation = ', Start: ' + selectedSequenceStart + ', End: ' + selectedSequenceEnd + ', Length: ' + sequenceLength;
	}
	
	var panelMarginBottom = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginBottom;
	var panelMarginTop = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginTop;
	var panelMarginLeft = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginLeft;
	
	var sequenceWidth = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.sequenceConf.widthPx;
	var seqFontSizePx = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.sequenceConf.fontSizePx;
	
	var viewPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.sequenceConf.labelPrefix;
	var panelContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.labelPrefix;
	
	var titleFontWeight = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.fontWeight;
	var titleFontSizePx = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.fontSizePx;
	var titleBorderBottom = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.borderBottom;
	var titleMarginBottom = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.marginBottom;
	var titleMarginTop = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.marginTop;
	var titleBackgroundColor = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.backgroundColor;
	var titleContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.labelPrefix;
	
	var repMarginLeft = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.leftPaddingPx;
	var chromosomeContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.labelPrefix;

	var container = new YAHOO.util.Element('sequenceView');

	var panel = document.createElement("div");
	var ePanel = new YAHOO.util.Element(panel);
	var ePanelPrefix = viewPrefix + panelContainerPrefix;
	ePanel.set('id', ePanelPrefix + '_' + selectedChromosome);
	ePanel.setStyle('margin-bottom', YAHOO.dasBrowser.util.addPxSuffix(panelMarginBottom));
	ePanel.setStyle('margin-top', YAHOO.dasBrowser.util.addPxSuffix(panelMarginTop));
	ePanel.setStyle('margin-left', YAHOO.dasBrowser.util.addPxSuffix(panelMarginLeft));
	
	var titleInformationContainer = document.createElement("span");
	var eTitleInformationContainer = new YAHOO.util.Element(titleInformationContainer);
	eTitleInformationContainer.setStyle('color', '#999999');
	eTitleInformationContainer.appendChild(document.createTextNode(titleInformation ));
	
	var title = document.createElement("div");
	var eTitle = new YAHOO.util.Element(title);
	var eTitlePrefix = viewPrefix + titleContainerPrefix;
	eTitle.set('id', eTitlePrefix + '_' + selectedChromosome);
	eTitle.setStyle('position', 'relative');
	eTitle.setStyle('background-color', titleBackgroundColor);
	eTitle.setStyle('margin-bottom', YAHOO.dasBrowser.util.addPxSuffix(titleMarginBottom));
	eTitle.setStyle('margin-top', YAHOO.dasBrowser.util.addPxSuffix(titleMarginTop));
	eTitle.setStyle('border-bottom', titleBorderBottom);
	eTitle.addClass('fontSize' + titleFontSizePx);
	eTitle.setStyle('font-weight', titleFontWeight);
	eTitle.appendChild(document.createTextNode(titleName));
	eTitle.appendChild(eTitleInformationContainer);
	ePanel.appendChild(eTitle);
	
	var sequenceCont = document.createElement("div");
	var eSequenceCont = new YAHOO.util.Element(sequenceCont);
	var eSequenceContPrefix = viewPrefix + chromosomeContainerPrefix;
	eSequenceCont.set('id', eSequenceContPrefix + '_' + selectedChromosome);
	eSequenceCont.setStyle('position', 'relative');
	eSequenceCont.setStyle('display', 'block');
	eSequenceCont.setStyle('margin-left', YAHOO.dasBrowser.util.addPxSuffix(repMarginLeft));
	eSequenceCont.setStyle('font-family', 'Courier, \'Courier New\', monospace');
	eSequenceCont.addClass('fontSize' + seqFontSizePx);
	eSequenceCont.setStyle('width',  YAHOO.dasBrowser.util.addPxSuffix(sequenceWidth));
	ePanel.appendChild(eSequenceCont);
	
	container.appendChild(ePanel);
	
	if(featureSequence){
		var sequenceObj = new Object();
		sequenceObj.dnaSequence = featureSequence;
		sequenceObj.featureId = selectedChromosome;
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawSequence(sequenceObj);
	} else {
		YAHOO.dasBrowser.genome.request.makeSequenceRequest(selectedChromosome, selectedSequenceStart, selectedSequenceEnd);
	}
}

/*
 * Draw the Sequence inside the sequence view
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawSequence = function(sequenceObj){
	var sequenceLength = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.sequenceConf.length;
	var viewPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.sequenceConf.labelPrefix;
	var chromosomeContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.labelPrefix;
	var eSequenceContPrefix = viewPrefix + chromosomeContainerPrefix;
	var selectedChromosome = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.sequenceConf.coordinates.chromosomeId;
	//var callType = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.sequenceConf.callType;
	if(sequenceObj.featureId){
		var container = new YAHOO.util.Element(eSequenceContPrefix + '_' + sequenceObj.featureId);
	} else {
		var container = new YAHOO.util.Element(eSequenceContPrefix + '_' + selectedChromosome);
	}
	

	var substringStart = 0;
	var substringEnd = sequenceLength;
	var i = 1;
	while (i > 0){
		var substring = sequenceObj.dnaSequence.substring(substringStart,substringEnd);
		container.appendChild(document.createTextNode(substring.toUpperCase()));
		container.appendChild(document.createElement("br"));
		substringStart = substringEnd
		substringEnd = substringEnd + sequenceLength;
		i = substring.length;
	}
}


/*
 * Draw layout for the annotation List view
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawAnnotationListViewStructure = function(selectedChromosome, selectedSource, selectedSequenceStart, selectedSequenceEnd, viewPrefix){
	
	var panelMarginBottom = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginBottom;
	var panelMarginTop = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginTop;
	var panelMarginLeft = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginLeft;
	
	var viewPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.annotationListConf.labelPrefix;
	var panelContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.labelPrefix;
	
	var titleFontWeight = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.fontWeight;
	var titleFontSizePx = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.fontSizePx;
	var titleBorderBottom = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.borderBottom;
	var titleMarginBottom = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.marginBottom;
	var titleMarginTop = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.marginTop;
	var titleBackgroundColor = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.backgroundColor;
	var titleContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.labelPrefix;
	
	var repMarginLeft = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.leftPaddingPx;
	var chromosomeContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.labelPrefix;
	
	var higherDefinitionView = selectedSource.result[selectedChromosome].higherDefinitionView;
	
	var annotationSource = YAHOO.dasBrowser.genome.annotations.sources[selectedSource.arrayPosition];
	if(annotationSource.representation == 'mark' || annotationSource.representation == 'track'){
		var topSimpleAnnotContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topSimpleAnnotationContainer.labelPrefix;
	} else if (annotationSource.representation == "chart" || annotationSource.representation == "line"){
		var topSimpleAnnotContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topSimpleChartAnnotationContainer.labelPrefix;
	}
	
	var selectedAnnotPrefix = chromosomeContainerPrefix + topSimpleAnnotContainerPrefix + '_' + selectedChromosome + '_s' + selectedSource.arrayPosition + '_';	
	var container = new YAHOO.util.Element('annotationListView');

	var panel = document.createElement("div");
	var ePanel = new YAHOO.util.Element(panel);
	var ePanelPrefix = viewPrefix + panelContainerPrefix;
	ePanel.set('id', ePanelPrefix + '_' + selectedChromosome);
	ePanel.setStyle('margin-bottom', YAHOO.dasBrowser.util.addPxSuffix(panelMarginBottom));
	ePanel.setStyle('margin-top', YAHOO.dasBrowser.util.addPxSuffix(panelMarginTop));
	ePanel.setStyle('margin-left', YAHOO.dasBrowser.util.addPxSuffix(panelMarginLeft));
	
	var chrIdName = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].id;
	var chrIdType = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].type;
	var sequenceLength = selectedSequenceEnd - (selectedSequenceStart-1);
	var titleName = ' ' + chrIdType.toUpperCase() + ' ' + chrIdName;
	var titleInformation = ', Start: ' + selectedSequenceStart + ', End: ' + selectedSequenceEnd + ', Length: ' + sequenceLength + ', Source: ' + selectedSource.name + ' (' + selectedSource.id + ')';
	
	var titleInformationContainer = document.createElement("span");
	var eTitleInformationContainer = new YAHOO.util.Element(titleInformationContainer);
	eTitleInformationContainer.setStyle('color', '#999999');
	eTitleInformationContainer.appendChild(document.createTextNode(titleInformation));
	
	var title = document.createElement("div");
	var eTitle = new YAHOO.util.Element(title);
	var eTitlePrefix = viewPrefix + titleContainerPrefix;
	eTitle.set('id', eTitlePrefix + '_' + selectedChromosome);
	eTitle.setStyle('position', 'relative');
	eTitle.setStyle('background-color', titleBackgroundColor);
	eTitle.setStyle('margin-bottom', YAHOO.dasBrowser.util.addPxSuffix(titleMarginBottom));
	eTitle.setStyle('margin-top', YAHOO.dasBrowser.util.addPxSuffix(titleMarginTop));
	eTitle.setStyle('border-bottom', titleBorderBottom);
	eTitle.addClass('fontSize' + titleFontSizePx);
	eTitle.setStyle('font-weight', titleFontWeight);
	eTitle.appendChild(document.createTextNode(titleName));
	eTitle.appendChild(eTitleInformationContainer);
	ePanel.appendChild(eTitle);


	var annotListCont = document.createElement("div");
	var eAnnotListCont = new YAHOO.util.Element(annotListCont);
	var eAnnotListContPrefix = viewPrefix + chromosomeContainerPrefix;
	eAnnotListCont.set('id', eAnnotListContPrefix + '_' + selectedChromosome);
	eAnnotListCont.setStyle('position', 'relative');
	eAnnotListCont.setStyle('display', 'block');
	eAnnotListCont.setStyle('margin-left', YAHOO.dasBrowser.util.addPxSuffix(repMarginLeft));
	
	//YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[elementId].kv.features
	var selectedAnnotations = new Array();
	//var sourceAnnotations = selectedSource.result[selectedChromosome];
	
	//console.log(annotationSource.representation);
	
	if(viewPrefix == "Kv"){
		var sourceAnnotations = selectedSource.result[selectedChromosome].kv.features;
	} else if(viewPrefix == "Cv"){
		if(higherDefinitionView != "Kv"){
			var sourceAnnotations = selectedSource.result[selectedChromosome].cv.features;
		} else {
			var sourceAnnotations = selectedSource.result[selectedChromosome].kv.features;
		}
	} else {
		if(higherDefinitionView == "Bv"){
			var sourceAnnotations = selectedSource.result[selectedChromosome].bv.features;
		} else if(higherDefinitionView == "Zv"){
			var sourceAnnotations = selectedSource.result[selectedChromosome].zv.features;
		} else if(higherDefinitionView == "Cv"){
			var sourceAnnotations = selectedSource.result[selectedChromosome].cv.features;
		} else {
			var sourceAnnotations = selectedSource.result[selectedChromosome].kv.features;
		}
	}

	//var higherDefinitionView = selectedSource.results[selectedChromosome].higherDefinitionView;
	for (var j = 0; j < sourceAnnotations.length; j++) {
		var tempCount1 = 0;
		if(parseInt(sourceAnnotations[j].start) < parseInt(selectedSequenceStart)){
			tempCount1++;
		}
		if(parseInt(sourceAnnotations[j].end) < parseInt(selectedSequenceStart)){
			tempCount1++;
		}
		var tempCount2 = 0;
		if(parseInt(sourceAnnotations[j].start) > parseInt(selectedSequenceEnd)){
			tempCount2++;
		}
		if(parseInt(sourceAnnotations[j].end) > parseInt(selectedSequenceEnd)){
			tempCount2++;
		}
		if (tempCount1 == 2 || tempCount2 == 2) {
			// Annotations out of range
		} else {
			var sourceAnnotation = sourceAnnotations[j];
			sourceAnnotation.test = selectedAnnotPrefix + sourceAnnotations[j].id + '_' + sourceAnnotations[j].start + '_' + sourceAnnotations[j].end;
			//eAnnotListCont.appendChild(document.createTextNode(sourceAnnotations[j].id));
			//eAnnotListCont.appendChild(document.createElement("br"));
			selectedAnnotations.push(sourceAnnotation);
		}
	}

	ePanel.appendChild(eAnnotListCont);
	container.appendChild(ePanel);


	this.myCustomFormatter = function(elCell, oRecord, oColumn, oData) {
    	var divContainer = oRecord.getData("test");
		var eContainer = elCell.parentNode.id;
		elCell.innerHTML = "<b>" + oData + "</b>";
		//addListener('click', highlightAnnotationClickCallback, divContainer);
		YAHOO.util.Event.addListener(eContainer, 'click', highlightAnnotationClickCallback, divContainer);

    };
        
    YAHOO.widget.DataTable.Formatter.myCustom = this.myCustomFormatter;

	
	var myColumnDefs = [
    	{key:"id", label:"Feature ID", formatter:"myCustom", sortable:true},
    	{key:"start", label:"Start", formatter:"number", sortable:true},
		{key:"end", label:"End", formatter:"number", sortable:true},
		{key:"typeId", label:"Type ID", sortable:true},
		//{key:"typeName", label:"Type name", sortable:true},
		{key:"typeCategory", label:"Category", sortable:true},
		{key:"methodId", label:"Method ID", sortable:true},
		//{key:"methodName", label:"Method Name", sortable:true},
		{key:"score", label:"Score", formatter:"number", sortable:true},
		{key:"orientation", label:"Orientation", sortable:true},
		{key:"test", label:"", hidden: true, width:0}
	];

	
	var myDataSource = new YAHOO.util.DataSource(selectedAnnotations);
	myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
	myDataSource.responseSchema = {
    	//fields: ["id","start","end", "typeId", "typeName", "typeCategory", "methodId", "methodName", "score", "orientation", "test"]
		fields: ["id","start","end", "typeId", "typeCategory", "methodId", "score", "orientation", "test"]
	};
	
	var myDataTable = new YAHOO.widget.DataTable(eAnnotListContPrefix + '_' + selectedChromosome, myColumnDefs, myDataSource, {sortedBy:{key:"start", dir:YAHOO.widget.DataTable.CLASS_ASC}});
	//var oColumn = myDataTable.getColumn(10); 
	//myDataTable.hideColumn(oColumn); 
	
	//myDataTable.set("selectionMode","single"); 
	myDataTable.subscribe("rowClickEvent", myDataTable.onEventSelectRow); 
	//myRowHighlightDataTable.subscribe("rowMouseoverEvent", myRowHighlightDataTable.onEventHighlightRow); 
	//myRowHighlightDataTable.subscribe("rowMouseoutEvent", myRowHighlightDataTable.onEventUnhighlightRow); 
	


}


/*
 * Highlight annotation displaying a blue border
 */
function highlightAnnotationClickCallback(e, divContainer) {
	var bandViewPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.labelPrefix;
	var zoomViewPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix;
	var chromosomeViewPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.labelPrefix;
	var karyotypeViewPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.labelPrefix;
	var viewPrefix = new Array();
	viewPrefix.push(bandViewPrefix);
	viewPrefix.push(zoomViewPrefix);
	viewPrefix.push(chromosomeViewPrefix);
	viewPrefix.push(karyotypeViewPrefix);
	
	for (var j = 0; j < viewPrefix.length; j++) {
		var eAnnotationContainer = new YAHOO.util.Element(viewPrefix[j] + divContainer);
		var opacity = eAnnotationContainer.getStyle('opacity');
		if (parseInt(opacity) == 1) {
			eAnnotationContainer.setStyle('outline', 'none');
			eAnnotationContainer.setStyle('opacity', 0.7);
			eAnnotationContainer.setStyle('z-index', '1');
		}
		else {
			eAnnotationContainer.setStyle('outline', '2px solid #426fd9');
			eAnnotationContainer.setStyle('opacity', 1);
			eAnnotationContainer.setStyle('z-index', '2')
		}
	}
}







/*
 * Draw layout for the band view
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawBandViewStructure = function(selectedChromosome, layout){
	var panelMarginBottom = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginBottom;
	var panelMarginTop = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginTop;
	var panelMarginLeft = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginLeft;
	var viewPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.labelPrefix;
	var container = new YAHOO.util.Element('bandView');
	var selectedBand = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.selectedBand;
	if (selectedBand == null) {
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.selectedBand = 0;
	}
	var eChrPanel = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawChromosomePanelStructure(viewPrefix, selectedChromosome, layout);
	eChrPanel.setStyle('margin-bottom', YAHOO.dasBrowser.util.addPxSuffix(panelMarginBottom));
	eChrPanel.setStyle('margin-top', YAHOO.dasBrowser.util.addPxSuffix(panelMarginTop));
	eChrPanel.setStyle('margin-left', YAHOO.dasBrowser.util.addPxSuffix(panelMarginLeft));
	container.appendChild(eChrPanel);
}

/*
 * Draw layout for the zoom view
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawZoomViewStructure = function(selectedChromosome, layout){
	var panelMarginBottom = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginBottom;
	var panelMarginTop = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginTop;
	var panelMarginLeft = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginLeft;
	//var viewPrefix = "Zv";
	//var viewPrefix = "Bv";
	var viewPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix;
	//YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.start = start;
	//YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.stop = stop;
	var container = new YAHOO.util.Element('zoomView');
	//var selectedBand = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.selectedBand;
	//if (selectedBand == null) {
	//	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.selectedBand = 0;
	//}

	var eChrPanel = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawChromosomePanelStructure(viewPrefix, selectedChromosome, layout);
	eChrPanel.setStyle('margin-bottom', YAHOO.dasBrowser.util.addPxSuffix(panelMarginBottom));
	eChrPanel.setStyle('margin-top', YAHOO.dasBrowser.util.addPxSuffix(panelMarginTop));
	eChrPanel.setStyle('margin-left', YAHOO.dasBrowser.util.addPxSuffix(panelMarginLeft));
	container.appendChild(eChrPanel);
}

/*
 * Draw layout for the chromosome view
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawChromosomeViewStructure = function(selectedChromosome, layout){
	
	var panelMarginBottom = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginBottom;
	var panelMarginTop = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginTop;
	var panelMarginLeft = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginLeft;
	
	var viewPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.labelPrefix;
	
	var container = new YAHOO.util.Element('chromosomeView');
	
	var eChrPanel = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawChromosomePanelStructure(viewPrefix, selectedChromosome, layout);
	eChrPanel.setStyle('margin-bottom', YAHOO.dasBrowser.util.addPxSuffix(panelMarginBottom));
	eChrPanel.setStyle('margin-top', YAHOO.dasBrowser.util.addPxSuffix(panelMarginTop));
	eChrPanel.setStyle('margin-left', YAHOO.dasBrowser.util.addPxSuffix(panelMarginLeft));
	container.appendChild(eChrPanel);

}

/*
 * Draw layout for the karyotype view
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawKaryotypeViewStructure = function(selectedEntryPoints, layout){
	
	var panelMarginBottom = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginBottom;
	var panelMarginTop = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginTop;
	var panelMarginLeft = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.marginLeft;
	
	var viewPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.labelPrefix;

	var columnPrefix = 'karyotypeViewColumn';
	
	
	var container = new YAHOO.util.Element('karyotypeView');
	//container.setStyle('display', 'block');
	
	var karyotypeColumns = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.karyotypeColumns;
	var columnWidth = panelMarginLeft + YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeLeftPaddingPx + YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.chromosomeWidthPx + 6;
	var columnMargin = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.karyotypeColumnsMargin;
	//var columnMarginLeft = columnWidth + YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.karyotypeColumnsMargin;
	for(var j = 0; j < karyotypeColumns; j++) {
		var columnContainer = document.createElement("div");
		var eColumnContainer = new YAHOO.util.Element(columnContainer);
		eColumnContainer.set('id', columnPrefix+j);
		if(j == 0){
			eColumnContainer.setStyle('position', 'relative');
			eColumnContainer.setStyle('float', 'left');
		} else {
			//eColumnContainer.setStyle('position', 'absolute');
			eColumnContainer.setStyle('position', 'relative');
			eColumnContainer.setStyle('float', 'left');
			
		}
		eColumnContainer.setStyle('top', '0px');
		//eColumnContainer.setStyle('left', YAHOO.dasBrowser.util.addPxSuffix(columnMarginLeft*j));
		eColumnContainer.setStyle('margin-right', YAHOO.dasBrowser.util.addPxSuffix(columnMargin));
		eColumnContainer.setStyle('width', YAHOO.dasBrowser.util.addPxSuffix(columnWidth));
		container.appendChild(eColumnContainer);
	}
	
	var countColumns = 0;
	var countColumnsTemp = 0;
	for (var j = 0; j < selectedEntryPoints.length; j++) {
		//console.log("forSEP"+selectedEntryPoints[j]);
		var eChrPanel = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawChromosomePanelStructure(viewPrefix, selectedEntryPoints[j], layout);
		eChrPanel.setStyle('margin-bottom', YAHOO.dasBrowser.util.addPxSuffix(panelMarginBottom));
		eChrPanel.setStyle('padding-top', YAHOO.dasBrowser.util.addPxSuffix(panelMarginTop));
		eChrPanel.setStyle('margin-left', YAHOO.dasBrowser.util.addPxSuffix(panelMarginLeft));
		eChrPanel.setStyle('clear', 'both');
		
		var columnContainerTemp = new YAHOO.util.Element(columnPrefix+countColumns);
		columnContainerTemp.appendChild(eChrPanel);
		countColumns++;
		countColumnsTemp = countColumns;
		if(countColumnsTemp >= karyotypeColumns){
			countColumns = 0;
		}
	}
}




/*
 * Draw layout for the panel container
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawChromosomePanelStructure = function(viewPrefix, selectedChromosome, layout){
	//console.log(viewPrefix+"dcps:"+selectedChromosome);
	
	var panelContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.labelPrefix;
	var titleContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.labelPrefix;
	var legendContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.legendContainer.labelPrefix;
	var detailsContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.detailsContainer.labelPrefix;
	var annotationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.annotationContainer.labelPrefix;
	var chromosomeContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.labelPrefix;
	var topBandLabelContainerLocation = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topBandLabelContainer.positionPrefix;
	var topBandLabelContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topBandLabelContainer.labelPrefix;
	var bottomBandLabelContainerLocation = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.bottomBandLabelContainer.positionPrefix;
	var bottomBandLabelContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.bottomBandLabelContainer.labelPrefix;
	var topSimpleAnnotationContainerLocation = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topSimpleAnnotationContainer.positionPrefix;
	var topSimpleAnnotationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topSimpleAnnotationContainer.labelPrefix;
	var topSimpleChartAnnotationContainerLocation = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topSimpleChartAnnotationContainer.positionPrefix;
	var topSimpleChartAnnotationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topSimpleChartAnnotationContainer.labelPrefix;
	var bottomSimpleChartAnnotationContainerLocation = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.bottomSimpleChartAnnotationContainer.positionPrefix;
	var bottomSimpleChartAnnotationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.bottomSimpleChartAnnotationContainer.labelPrefix;
	var bottomSimpleAnnotationContainerLocation = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.bottomSimpleAnnotationContainer.positionPrefix;
	var bottomSimpleAnnotationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.bottomSimpleAnnotationContainer.labelPrefix;
	var chromosomeRepresentationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.chromosomeRepresentationContainer.labelPrefix;
	var chromosomeBandContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.chromosomeRepresentationContainer.chromosomeBandContainer.labelPrefix;
	var chromosomeLegendContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.sourceLegendContainer.labelPrefix;
	var chromosomeSliderContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.sliderContainer.labelPrefix;
	if (viewPrefix == 'Zv'){
		 var zoomStart = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.start;
	 	 var zoomStop = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.stop;
		 var zoomLength = zoomStop - (zoomStart-1);
		 var titleInformation = ', Start: '+ zoomStart + ', End: '+ zoomStop + ', Length: '+ zoomLength;
	} else if (viewPrefix == 'Bv') {
		 var selectedBand = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.selectedBand;
		 var selectedBandIdName = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosome].bands[selectedBand].id;
		 var selectedBandIdStart = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosome].bands[selectedBand].start;
		 var selectedBandIdEnd = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosome].bands[selectedBand].end;
		 var selectedBandIdLength = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosome].bands[selectedBand].length;
		 var titleInformation = ', Start: '+selectedBandIdStart + ', End: '+selectedBandIdEnd + ', Length: '+selectedBandIdLength + ', Band: ' + selectedBandIdName;
	} else {
		 var chromosomeStart = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].start;
	 	 var chromosomeStop = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].stop;
		 var chromosomeLength = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].length;
		 var titleInformation = ', Start: '+ chromosomeStart + ', End: '+ chromosomeStop + ', Length: '+ chromosomeLength;
		 if (viewPrefix != 'Cv' && YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.lightTitle) {
		 	titleInformation = '';
		 }
	}
	
	var titleFontWeight = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.fontWeight;
	var titleFontSizePx = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.fontSizePx;
	var titleBorderBottom = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.borderBottom;
	var titleMarginBottom = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.marginBottom;
	var titleMarginTop = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.marginTop;
	var titleBackgroundColor = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.backgroundColor;
	var repMarginLeft = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.leftPaddingPx;
	
	if(viewPrefix == YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.labelPrefix) {
		var topBandLabelContainer_visible = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topBandLabelContainer.visible_Cv;
		var bottomBandLabelContainer_visible = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.bottomBandLabelContainer.visible_Cv;
	} else if (viewPrefix == YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.labelPrefix){
		var topBandLabelContainer_visible = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topBandLabelContainer.visible_Kv;
		var bottomBandLabelContainer_visible = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.bottomBandLabelContainer.visible_Kv;
	} else if (viewPrefix == YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix){
		var topBandLabelContainer_visible = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topBandLabelContainer.visible_Zv;
		var bottomBandLabelContainer_visible = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.bottomBandLabelContainer.visible_Zv;
	} else {
		var topBandLabelContainer_visible = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topBandLabelContainer.visible_Bv;
		var bottomBandLabelContainer_visible = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.bottomBandLabelContainer.visible_Bv;
	}
	
	var chrPanel = document.createElement("div");
	var eChrPanel = new YAHOO.util.Element(chrPanel);
	var eChrPanelPrefix = viewPrefix + panelContainerPrefix;
	eChrPanel.set('id', eChrPanelPrefix + '_' + selectedChromosome);
	
	var titleInformationContainer = document.createElement("span");
	var eTitleInformationContainer = new YAHOO.util.Element(titleInformationContainer);
	eTitleInformationContainer.setStyle('color', '#999999');
	eTitleInformationContainer.appendChild(document.createTextNode(titleInformation));
	
	//layout: ['title','sourceLegend','details','chromosomeContainer','annotations']
	for (var i = 0; i < layout.length; i++) {
		if (layout[i] == 'title') {
			var chrIdName = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].id;
			var chrIdType = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].type;
			var chrTitle = document.createElement("div");
			var eChrTitle = new YAHOO.util.Element(chrTitle);
			var eChrTitlePrefix = viewPrefix + titleContainerPrefix;
			eChrTitle.set('id', eChrTitlePrefix + '_' + selectedChromosome);
			eChrTitle.setStyle('position', 'relative');
			eChrTitle.setStyle('background-color', titleBackgroundColor);
			eChrTitle.setStyle('margin-bottom', YAHOO.dasBrowser.util.addPxSuffix(titleMarginBottom));
			eChrTitle.setStyle('margin-top', YAHOO.dasBrowser.util.addPxSuffix(titleMarginTop));
			eChrTitle.setStyle('border-bottom', titleBorderBottom);
			eChrTitle.addClass('fontSize' + titleFontSizePx);
			eChrTitle.setStyle('font-weight', titleFontWeight);
			eChrTitle.appendChild(document.createTextNode(' ' + chrIdType.toUpperCase() + ' ' + chrIdName));
			eChrTitle.appendChild(eTitleInformationContainer);
			eChrPanel.appendChild(eChrTitle);
		}
		else if (layout[i] == 'sourceLegend') {
			var ChrLegend = document.createElement("div");
			var eChrLegend = new YAHOO.util.Element(ChrLegend);
			var eChrLegendPrefix = viewPrefix + legendContainerPrefix;
			eChrLegend.set('id', eChrLegendPrefix + '_' + selectedChromosome);
			eChrLegend.setStyle('position', 'relative');
			eChrLegend.setStyle('display', 'block');
			eChrPanel.appendChild(eChrLegend);
		}
		else if (layout[i] == 'details') {
			var chrDetails = document.createElement("div");
			var eChrDetails = new YAHOO.util.Element(chrDetails);
			var eChrDetailsPrefix = viewPrefix + detailsContainerPrefix;
			eChrDetails.set('id', eChrDetailsPrefix + '_' + selectedChromosome);
			eChrDetails.setStyle('position', 'relative');
			//eChrDetails.setStyle('height', '10px');
			//eChrDetails.setStyle('border-top', '1px solid #ffffff');
			eChrDetails.setStyle('clear', 'both');
			eChrPanel.appendChild(eChrDetails);
		}
		else if (layout[i] == 'chromosomeContainer') {
			var ChrCont = document.createElement("div");
			var eChrCont = new YAHOO.util.Element(ChrCont);
			var eChrContPrefix = viewPrefix + chromosomeContainerPrefix;
			eChrCont.set('id', eChrContPrefix + '_' + selectedChromosome);
			eChrCont.setStyle('position', 'relative');
			eChrCont.setStyle('display', 'block');
			eChrCont.setStyle('margin-left', YAHOO.dasBrowser.util.addPxSuffix(repMarginLeft));
			eChrCont.setStyle('clear', 'both');

			var ChrContLayout = ['topBandLabels','topSimpleAnnotations','topSimpleChartAnnotations' ,'chromosomeRepresentation','bottomSimpleChartAnnotations' ,'bottomSimpleAnnotations', 'bottomBandLabels'];
			//var ChrContLayout = ['topBandLabels','topSimpleAnnotations','topSimpleChartAnnotations' ,'chromosomeRepresentation','bottomSimpleChartAnnotations' ,'bottomSimpleAnnotations', 'bottomBandLabels', 'sourceLegend'];
			for (var j = 0; j < ChrContLayout.length; j++) {
				if (ChrContLayout[j] == 'topBandLabels') {
					var ChrContTopBandLabel = document.createElement("div");
					var eChrContTopBandLabel = new YAHOO.util.Element(ChrContTopBandLabel);
					var eChrContTopBandLabelPrefix = eChrContPrefix + topBandLabelContainerLocation + topBandLabelContainerPrefix;
					eChrContTopBandLabel.set('id', eChrContTopBandLabelPrefix + '_' + selectedChromosome);
					eChrContTopBandLabel.setStyle('position', 'relative');
					if(topBandLabelContainer_visible){
						eChrContTopBandLabel.setStyle('display', 'block');
					} else {
						eChrContTopBandLabel.setStyle('display', 'none');
					}
					eChrCont.appendChild(eChrContTopBandLabel);
				}
				else if (ChrContLayout[j] == 'topSimpleAnnotations') {
					var ChrContTopSimpleAnnotation = document.createElement("div");
					var eChrContTopSimpleAnnotation = new YAHOO.util.Element(ChrContTopSimpleAnnotation);
					var eChrContTopSimpleAnnotationPrefix = eChrContPrefix + topSimpleAnnotationContainerLocation + topSimpleAnnotationContainerPrefix;
					eChrContTopSimpleAnnotation.set('id', eChrContTopSimpleAnnotationPrefix + '_' + selectedChromosome);
					eChrContTopSimpleAnnotation.setStyle('position', 'relative');
					eChrContTopSimpleAnnotation.setStyle('padding-left', '1px');
					eChrCont.appendChild(eChrContTopSimpleAnnotation);
				}
				else if (ChrContLayout[j] == 'topSimpleChartAnnotations') {
					var ChrContTopSimpleChartAnnotation = document.createElement("div");
					var eChrContTopSimpleChartAnnotation = new YAHOO.util.Element(ChrContTopSimpleChartAnnotation);
					var eChrContTopSimpleChartAnnotationPrefix = eChrContPrefix + topSimpleChartAnnotationContainerLocation + topSimpleChartAnnotationContainerPrefix;
					eChrContTopSimpleChartAnnotation.set('id', eChrContTopSimpleChartAnnotationPrefix + '_' + selectedChromosome);
					eChrContTopSimpleChartAnnotation.setStyle('position', 'relative');
					eChrContTopSimpleChartAnnotation.setStyle('padding-left', '1px');
					eChrCont.appendChild(eChrContTopSimpleChartAnnotation);
				}
				else if (ChrContLayout[j] == 'chromosomeRepresentation') {
					var ChrContChrRep = document.createElement("div");
					var eChrContChrRep = new YAHOO.util.Element(ChrContChrRep);
					var eChrContChrRepPrefix = eChrContPrefix + chromosomeRepresentationContainerPrefix;
					eChrContChrRep.set('id',  eChrContChrRepPrefix + '_' + selectedChromosome);
					eChrContChrRep.setStyle('position', 'relative');
					eChrContChrRep.setStyle('z-index', '2');
					eChrCont.appendChild(eChrContChrRep);
					
					var ChrContChrRepBands = document.createElement("div");
					var eChrContChrRepBands = new YAHOO.util.Element(ChrContChrRepBands);
					var eChrContChrRepBandsPrefix = eChrContChrRepPrefix + chromosomeBandContainerPrefix;
					eChrContChrRepBands.set('id', eChrContChrRepBandsPrefix + '_' + selectedChromosome);
					eChrContChrRepBands.setStyle('position', 'absolute');
					eChrContChrRep.appendChild(eChrContChrRepBands);
				}
				else if (ChrContLayout[j] == 'bottomSimpleChartAnnotations') {
					var ChrContBottomSimpleChartAnnotation = document.createElement("div");
					var eChrContBottomSimpleChartAnnotation = new YAHOO.util.Element(ChrContBottomSimpleChartAnnotation);
					var eChrContBottomSimpleChartAnnotationPrefix = eChrContPrefix + bottomSimpleChartAnnotationContainerLocation + bottomSimpleChartAnnotationContainerPrefix;
					eChrContBottomSimpleChartAnnotation.set('id', eChrContBottomSimpleChartAnnotationPrefix + '_' + selectedChromosome);
					eChrContBottomSimpleChartAnnotation.setStyle('position', 'relative');
					eChrContBottomSimpleChartAnnotation.setStyle('padding-left', '1px');
					eChrCont.appendChild(eChrContBottomSimpleChartAnnotation);
				}
				else if (ChrContLayout[j] == 'bottomSimpleAnnotations') {
					var ChrContBottomSimpleAnnotation = document.createElement("div");
					var eChrContBottomSimpleAnnotation = new YAHOO.util.Element(ChrContBottomSimpleAnnotation);
					var eChrContBottomSimpleAnnotationPrefix = eChrContPrefix + bottomSimpleAnnotationContainerLocation + bottomSimpleAnnotationContainerPrefix;
					eChrContBottomSimpleAnnotation.set('id', eChrContBottomSimpleAnnotationPrefix + '_' + selectedChromosome);
					eChrContBottomSimpleAnnotation.setStyle('position', 'relative');
					eChrContBottomSimpleAnnotation.setStyle('padding-left', '1px');
					eChrCont.appendChild(eChrContBottomSimpleAnnotation);
				}
				else if (ChrContLayout[j] == 'bottomBandLabels') {
					var ChrContBottomBandLabel = document.createElement("div");
					var eChrContBottomBandLabel = new YAHOO.util.Element(ChrContBottomBandLabel);
					var eChrContBottomBandLabelPrefix = eChrContPrefix + bottomBandLabelContainerLocation + bottomBandLabelContainerPrefix;
					eChrContBottomBandLabel.set('id', eChrContBottomBandLabelPrefix + '_' + selectedChromosome);
					eChrContBottomBandLabel.setStyle('position', 'relative');
					if(bottomBandLabelContainer_visible){
						eChrContBottomBandLabel.setStyle('display', 'block');
					} else {
						eChrContBottomBandLabel.setStyle('display', 'none');
					}
					eChrCont.appendChild(eChrContBottomBandLabel);
				}
				/*
				else if (ChrContLayout[j] == 'sourceLegend') {
					var ChrContLegend = document.createElement("div");
					var eChrContLegend = new YAHOO.util.Element(ChrContLegend);
					var eChrContLegendPrefix = eChrContPrefix + chromosomeLegendContainerPrefix;
					eChrContLegend.set('id', eChrContLegendPrefix + '_' + selectedChromosome);
					eChrContLegend.setStyle('position', 'relative');
					eChrContLegend.setStyle('display', 'block');
					eChrCont.appendChild(eChrContLegend);
				}
				*/
			}
			eChrPanel.appendChild(eChrCont);
		}
		else if (layout[i] == 'annotations') {
			var chrAnnotation = document.createElement("div");
			var eChrAnnotation = new YAHOO.util.Element(chrAnnotation);
			var eChrAnnotationPrefix = viewPrefix + annotationContainerPrefix;
			eChrAnnotation.set('id', eChrAnnotationPrefix + '_' + selectedChromosome);
			eChrAnnotation.setStyle('position', 'relative');
			//eChrAnnotation.setStyle('height', '10px');
			//eChrAnnotation.setStyle('border-top', '1px solid #ffffff');
			eChrPanel.appendChild(eChrAnnotation);
		}
		else if (layout[i] == 'slider') {
			var chrSlider = document.createElement("div");
			var eChrSlider = new YAHOO.util.Element(chrSlider);
			var eChrSliderPrefix = viewPrefix + chromosomeSliderContainerPrefix;
			eChrSlider.set('id', eChrSliderPrefix + '_' + selectedChromosome);
			eChrSlider.setStyle('position', 'relative');
			eChrSlider.setStyle('margin-left', YAHOO.dasBrowser.util.addPxSuffix(repMarginLeft-10));
			//eChrSlider.setStyle('height', '10px');
			//eChrSlider.setStyle('border-top', '1px solid #ffffff');
			eChrPanel.appendChild(eChrSlider);
		}
	}
	return eChrPanel;
}

/*
 * Draw karyotype components * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 */

/*
 *  Draw band
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawBand = function(chrId, chrPrefix, widthPx, heightPx){
	var widthPx = YAHOO.dasBrowser.util.addPxSuffix(widthPx);
	var heightPx = YAHOO.dasBrowser.util.addPxSuffix(heightPx);
	var chromosomeContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.labelPrefix;
	var chromosomeRepresentationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.chromosomeRepresentationContainer.labelPrefix;
	var karyotypeStylesheet = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.stylesheet.valuesAndColors;
	
	var selectedBandId = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.selectedBand;
	var selectedBandObject = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[chrId].bands[selectedBandId];
	var bandColor = karyotypeStylesheet[selectedBandObject.typeId];
	
	var eChrPrefix = chrPrefix + chromosomeContainerPrefix + chromosomeRepresentationContainerPrefix;
	eChr = new YAHOO.util.Element(eChrPrefix + '_' + chrId);
	eChr.setStyle('background-color', bandColor);
	eChr.setStyle('width', widthPx);
	eChr.setStyle('height', heightPx);
	eChr.setStyle('border', '1px solid #999999');
}

/*
 *  Draw empty chromosome
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawChromosome = function(chrId, chrPrefix, widthPx, heightPx){
	var widthPx = YAHOO.dasBrowser.util.addPxSuffix(widthPx);
	var heightPx = YAHOO.dasBrowser.util.addPxSuffix(heightPx);
	var chromosomeContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.labelPrefix;
	var chromosomeRepresentationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.chromosomeRepresentationContainer.labelPrefix;

	var eChrPrefix = chrPrefix + chromosomeContainerPrefix + chromosomeRepresentationContainerPrefix;
	eChr = new YAHOO.util.Element(eChrPrefix + '_' + chrId);
	eChr.setStyle('background-color', '#f2f2f2');
	eChr.setStyle('width', widthPx);
	eChr.setStyle('height', heightPx);
	eChr.setStyle('border', '1px solid #999999');
	eChr.setStyle('cursor', 'pointer');
	//eChr.addClass('gr_div');
}


/*
 *  Draw empty Zoom region
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawZoom = function(chrId, zoomPrefix, widthPx, heightPx){
	var widthPx = YAHOO.dasBrowser.util.addPxSuffix(widthPx);
	var heightPx = YAHOO.dasBrowser.util.addPxSuffix(heightPx);
	var chromosomeContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.labelPrefix;
	var chromosomeRepresentationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.chromosomeRepresentationContainer.labelPrefix;

	var eZoomPrefix = zoomPrefix + chromosomeContainerPrefix + chromosomeRepresentationContainerPrefix;
	eZoom = new YAHOO.util.Element(eZoomPrefix + '_' + chrId);
	eZoom.setStyle('background-color', '#f2f2f2');
	eZoom.setStyle('width', widthPx);
	eZoom.setStyle('height', heightPx);
	eZoom.setStyle('border', '1px solid #999999');
	eZoom.setStyle('cursor', 'pointer');
	//eZoom.addClass('gr_div');
}


/*
 *  Draw chromosome bands
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawChromosomeBands = function(chrId, chrPrefix, widthPx, heightPx){
	// Set variables
	var widthNumber = YAHOO.dasBrowser.util.deletePxSuffix(widthPx);
	var heightNumber = YAHOO.dasBrowser.util.deletePxSuffix(heightPx);
	var widthPx = YAHOO.dasBrowser.util.addPxSuffix(widthPx);
	var heightPx = YAHOO.dasBrowser.util.addPxSuffix(heightPx);
	var entryPoints = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[chrId];
	var chromosomeBands = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[chrId].bands;
	var karyotypeStylesheet = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.stylesheet.valuesAndColors;
	var acenPresent = 0;
	var labelCount = 0;
	var fontSizePx = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.labelFontSizePx;
	var panelContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.labelPrefix;
	var chromosomeContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.labelPrefix;
	var topBandLabelContainerLocation = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topBandLabelContainer.positionPrefix;
	var topBandLabelContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topBandLabelContainer.labelPrefix;
	var bottomBandLabelContainerLocation = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.bottomBandLabelContainer.positionPrefix;
	var bottomBandLabelContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.bottomBandLabelContainer.labelPrefix;
	var chromosomeRepresentationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.chromosomeRepresentationContainer.labelPrefix;
	var chromosomeBandContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.chromosomeRepresentationContainer.chromosomeBandContainer.labelPrefix;
	var labelLines = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.getNumberOfChromosomeLabelLines(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeLabelLinesPerPx, widthNumber);
	var panelContainerBgColor = YAHOO.dasBrowser.util.getDivBackgroundColor(chrPrefix + panelContainerPrefix + '_' + chrId);
	var bandLabelContainerHeight = YAHOO.dasBrowser.util.addPxSuffix((labelLines * YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.labelFontSizePx) + 2);
	
	// Set containers
	var eChrLabelTopContainerPrefix = chrPrefix + chromosomeContainerPrefix + topBandLabelContainerLocation + topBandLabelContainerPrefix;
	var eChrLabelTopContainer = new YAHOO.util.Element(eChrLabelTopContainerPrefix + '_' + chrId);
	eChrLabelTopContainer.setStyle('width', widthPx);
	eChrLabelTopContainer.setStyle('height', bandLabelContainerHeight);
	
	var eChrLabelBottomContainerPrefix = chrPrefix + chromosomeContainerPrefix + bottomBandLabelContainerLocation + bottomBandLabelContainerPrefix;
	var eChrLabelBottomContainer = new YAHOO.util.Element(eChrLabelBottomContainerPrefix + '_' + chrId);
	eChrLabelBottomContainer.setStyle('width', widthPx);
	eChrLabelBottomContainer.setStyle('height', bandLabelContainerHeight);
	
	var eChrBandContainerPrefix = chrPrefix + chromosomeContainerPrefix + chromosomeRepresentationContainerPrefix + chromosomeBandContainerPrefix;
	var eChrBandContainer = new YAHOO.util.Element(eChrBandContainerPrefix + '_' + chrId);
	if (chrPrefix == 'Cv' || chrPrefix == 'Zv') {
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.mappingBandNamesToBandIds = new Array();
	}
	
	if(YAHOO.lang.isUndefined(chromosomeBands)==false){
		for (var j = 0; j < chromosomeBands.length; j++) {
			var bandDisplay = true;
			if (chrPrefix == 'Zv') {
				var zoomStart = parseInt(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.start);
				var zoomStop = parseInt(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.stop);
				var zoomFeatureStartSeq = chromosomeBands[j].start;
				var zoomFeatureEndSeq = chromosomeBands[j].end;
				if (chromosomeBands[j].start > zoomStart && chromosomeBands[j].end < zoomStop) {
					//nothing
				}
				else 
					if (chromosomeBands[j].end > zoomStart && chromosomeBands[j].end < zoomStop) {
						var zoomFeatureStartSeq = zoomStart;
					}
					else 
						if (chromosomeBands[j].start > zoomStart && chromosomeBands[j].start < zoomStop) {
							var zoomFeatureEndSeq = zoomStop;
						}
						else 
							if (chromosomeBands[j].start <= zoomStart && chromosomeBands[j].end >= zoomStop) {
								var zoomFeatureEndSeq = zoomStop;
								var zoomFeatureStartSeq = zoomStart;
							}
							else {
								bandDisplay = false;
							}
			}
			if (bandDisplay == true) {
				// Create mapping BandNames - BandIds
				//if (chrPrefix == 'Cv' || chrPrefix == 'Zv') {
				if (chrPrefix == 'Cv') {
					YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.mappingBandNamesToBandIds[chromosomeBands[j].id] = j;
					YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.addRedrawBandListener(chrId, j, "chromosome");
				}
				
				var bandColor = karyotypeStylesheet[chromosomeBands[j].typeId];
				//if(bandColor=="undefined"){...}
				
				if (chrPrefix != 'Zv') {
					// Create coordinates object for a band
					var bandCoordinates = new myFeaturePosition();
					bandCoordinates.setReferenceLengthPx(widthPx);
					bandCoordinates.setFeatureStartSeq(chromosomeBands[j].start);
					bandCoordinates.setFeatureEndSeq(chromosomeBands[j].end);
					bandCoordinates.setReferenceStartSeq(entryPoints.start);
					bandCoordinates.setReferenceEndSeq(entryPoints.stop);
					bandCoordinates.calculatePx();
				}
				else {
					YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.addRedrawBandListener(chrId, j, "zoom");
					// Create coordinates object for a band
					var bandCoordinates = new myFeaturePosition();
					bandCoordinates.setReferenceLengthPx(widthPx);
					bandCoordinates.setFeatureStartSeq(zoomFeatureStartSeq);
					bandCoordinates.setFeatureEndSeq(zoomFeatureEndSeq);
					bandCoordinates.setReferenceStartSeq(zoomStart);
					bandCoordinates.setReferenceEndSeq(zoomStop);
					bandCoordinates.calculatePx();
				}
				
				
				
				// Create bands
				var band = document.createElement("a");
				var eBand = new YAHOO.util.Element(band);
				var eBandPrefix = eChrBandContainerPrefix;
				eBand.set('id', eBandPrefix + '_' + chrId + '_' + chromosomeBands[j].id);
				eBand.set('title', chromosomeBands[j].id);
				eBand.setStyle('background-color', bandColor);
				eBand.setStyle('width', YAHOO.dasBrowser.util.addPxSuffix(bandCoordinates.featureLengthPx));
				eBand.setStyle('height', heightPx);
				eBand.setStyle('top', '0px');
				eBand.setStyle('left', YAHOO.dasBrowser.util.addPxSuffix(bandCoordinates.featureStartPx));
				eBand.setStyle('position', 'absolute');
				eBand.appendChild(document.createTextNode(" "));
				
				//Modify the look of the "acen" and "stalk" bands and add a white background.
				if (chromosomeBands[j].typeId == "band:acen") {
					acenPresent = acenPresent + 1;
					var acenBg = document.createElement("a");
					var eAcenBg = new YAHOO.util.Element(acenBg);
					var eAcenBgPrefix = eChrBandContainerPrefix;
					eAcenBg.set('id', eAcenBgPrefix + '_' + chrId + '_' + chromosomeBands[j].id + '_bg');
					eAcenBg.setStyle('background-color', panelContainerBgColor);
					eAcenBg.setStyle('left', YAHOO.dasBrowser.util.addPxSuffix(bandCoordinates.featureStartPx));
					eAcenBg.setStyle('width', YAHOO.dasBrowser.util.addPxSuffix(bandCoordinates.featureLengthPx));
					eAcenBg.setStyle('top', '-1px');
					eAcenBg.setStyle('height', YAHOO.dasBrowser.util.addPxSuffix(parseInt(heightNumber) + 2));
					eAcenBg.setStyle('position', 'absolute');
					eChrBandContainer.appendChild(eAcenBg);
					var acenHeight = parseInt(heightNumber) * (2 / 5);
					var acenHeightTemp = acenHeight;
					if (acenHeightTemp < 1) {
						acenHeight = 1
					};
					eBand.setStyle('height', YAHOO.dasBrowser.util.addPxSuffix(acenHeight));
					eBand.setStyle('top', YAHOO.dasBrowser.util.addPxSuffix(parseInt(heightNumber) * (3 / 5) / 2));
				}
				else 
					if (chromosomeBands[j].typeId == "band:stalk") {
						var stalkBg = document.createElement("a");
						var eStalkBg = new YAHOO.util.Element(stalkBg);
						var eStalkBgPrefix = eChrBandContainerPrefix;
						eStalkBg.set('id', eStalkBgPrefix + '_' + chrId + '_' + chromosomeBands[j].id + '_bg');
						eStalkBg.setStyle('background-color', panelContainerBgColor);
						eStalkBg.setStyle('left', YAHOO.dasBrowser.util.addPxSuffix(bandCoordinates.featureStartPx));
						eStalkBg.setStyle('width', YAHOO.dasBrowser.util.addPxSuffix(bandCoordinates.featureLengthPx));
						eStalkBg.setStyle('top', '-1px');
						eStalkBg.setStyle('height', YAHOO.dasBrowser.util.addPxSuffix(parseInt(heightNumber) + 2));
						eStalkBg.setStyle('position', 'absolute');
						eChrBandContainer.appendChild(eStalkBg);
						eBand.setStyle('height', YAHOO.dasBrowser.util.addPxSuffix(parseInt(heightNumber) * (5 / 6)));
						eBand.setStyle('top', YAHOO.dasBrowser.util.addPxSuffix(parseInt(heightNumber) * (1 / 6) / 2));
					}
				
				//Create text for a band using the band Ids.
				if (labelLines != 0) {
					//if (acenPresent > 1 && chromosomeBands[j].typeId == "band:acen") {
					// This chomosome band label is duplicated, so do not show it on the graphic
					//}else{}
					var fontSizeCss = 'fontSize' + fontSizePx;
					var oddNumber = YAHOO.dasBrowser.util.isItanOddNumber(j);
					var labelCountTemp = labelCount;
					var topCorrection = fontSizePx * parseInt(labelCount);
					var bandText = document.createElement("span");
					var eBandText = new YAHOO.util.Element(bandText);
					eBandText.setStyle('top', YAHOO.dasBrowser.util.addPxSuffix(topCorrection));
					eBandText.setStyle('left', YAHOO.dasBrowser.util.addPxSuffix(bandCoordinates.featureStartPx));
					eBandText.setStyle('position', 'absolute');
					eBandText.addClass('fontSize' + fontSizePx);
					eBandText.appendChild(document.createTextNode(chromosomeBands[j].id));
					
					if (oddNumber) {
						var eBandTextPrefix = chrPrefix + chromosomeContainerPrefix + topBandLabelContainerPrefix;
						eBandText.set('id', eBandTextPrefix + '_' + chrId + '_' + chromosomeBands[j].id);
						eChrLabelTopContainer.appendChild(eBandText);
					}
					else {
						var eBandTextPrefix = chrPrefix + chromosomeContainerPrefix + bottomBandLabelContainerPrefix;
						eBandText.set('id', eBandTextPrefix + '_' + chrId + '_' + chromosomeBands[j].id);
						eChrLabelBottomContainer.appendChild(eBandText);
					}
					
					labelCount = labelCount + (1 / 2);
					if (labelCountTemp == (labelLines - 1) + (1 / 2)) {
						labelCount = 0;
					}
				}
				eChrBandContainer.appendChild(eBand);
			} // if (bandDisplay == true)
		} //for (var j = 0; j < chromosomeBands.length; j++)
	}
}

/*
 * Draw legend
 */
YAHOO.dasBrowser.genome.annotations.simpleView.drawLegend = function(viewPrefix, selectedChromosome, sourceArrayNumber, positionalAnnotations){
	var titleFontSizePx = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.titleContainer.fontSizePx;
	var chromosomeLegendContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.sourceLegendContainer.labelPrefix;
	var chromosomeContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.labelPrefix;
	var viewContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.labelPrefix;
	var legendContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.legendContainer.labelPrefix;
	var legendContainerName = viewPrefix + legendContainerPrefix + '_' + selectedChromosome;
	var annotationSource = YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber];

	if (viewPrefix == YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.labelPrefix) {
		//var positionalAnnotations = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.featureNumber;
	} else if (viewPrefix == YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix) {
		//var positionalAnnotations = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.featureNumber;
	}
	if(viewPrefix == "Cv"){
		if(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment == null){
			if(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment == null){
				var totalPositionalAnnotations = 0;
			} else {
				var totalPositionalAnnotations = YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment.featureNumber;
			}
		} else {
			if(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment == null){
				var totalPositionalAnnotations = 0;
			} else {
				var totalPositionalAnnotations = YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment.featureNumber;
			}
		}
		
		if(totalPositionalAnnotations != 0 && positionalAnnotations != totalPositionalAnnotations){
			var positionalAnnotations = positionalAnnotations + "/" + totalPositionalAnnotations;		
		}
	} else if(viewPrefix == "Kv"){
		if(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment == null){
			var totalPositionalAnnotations = 0;
		} else {
			var totalPositionalAnnotations = YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment.featureNumber;
		}
		if(totalPositionalAnnotations != 0 && positionalAnnotations != totalPositionalAnnotations){
			var positionalAnnotations = positionalAnnotations + "/" + totalPositionalAnnotations;		
		}
	}
	

	if (annotationSource.representation == 'mark' || annotationSource.representation == 'track') {
		if (annotationSource.align == "bottom") {
			var simpleAnnotationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.bottomSimpleAnnotationContainer.labelPrefix;
		}
		else {
			var simpleAnnotationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topSimpleAnnotationContainer.labelPrefix;
		}
	} else if (annotationSource.representation == "chart" || annotationSource.representation == "line") {
		if (annotationSource.align == "bottom") {
			var simpleAnnotationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.bottomSimpleChartAnnotationContainer.labelPrefix;
		}
		else {
			var simpleAnnotationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.topSimpleChartAnnotationContainer.labelPrefix;
		}
	}
	
	var annotationContainerName = viewPrefix + chromosomeContainerPrefix + simpleAnnotationContainerPrefix + '_' + selectedChromosome + '_s' + sourceArrayNumber;
	
	//console.log(legendContainerName);
	//BvContSAnnot_chromosome1_s0
	
	var legendColor = document.createElement("div");
	var eLegendColor = new YAHOO.util.Element(legendColor);
	eLegendColor.set('id', legendContainerName + '_s' + sourceArrayNumber + '_color');
	eLegendColor.setStyle('height', '7px');
	eLegendColor.setStyle('width', '9px');
	eLegendColor.setStyle('margin-top', '2px');
	eLegendColor.setStyle('margin-right', '3px');
	eLegendColor.setStyle('border-bottom', '1px solid #000000');
	eLegendColor.setStyle('border-top', '1px solid #000000');
	eLegendColor.setStyle('background-color', '#'+annotationSource.color);
	eLegendColor.setStyle('float', 'left');
	eLegendColor.setStyle('position', 'relative');
	eLegendColor.appendChild(document.createTextNode(" "));
	
	
	var featureNumber = document.createElement("span");
	var eFeatureNumber = new YAHOO.util.Element(featureNumber);
	eFeatureNumber.setStyle('color', '#666666');
	eFeatureNumber.appendChild(document.createTextNode("(" + positionalAnnotations + ")"));
	
	var legendName = document.createElement("a");
	var eLegendName = new YAHOO.util.Element(legendName);
	eLegendName.set('id', legendContainerName + '_s' + sourceArrayNumber + '_name');
	eLegendName.addClass('fontSize' + titleFontSizePx);
	//eLegendName.setStyle('href', '');
	eLegendName.setStyle('top', '1px');
	//eLegendName.setStyle('left', '0px');
	eLegendName.setStyle('position', 'relative');
	eLegendName.setStyle('float', 'left');
	eLegendName.setStyle('cursor', 'pointer');
	eLegendName.setStyle('text-decoration','underline');
	eLegendName.setStyle('color','#003DF5');
	eLegendName.appendChild(legendColor);
	if(viewPrefix != viewContainerPrefix){
		eLegendName.appendChild(document.createTextNode(annotationSource.name + " "));
	}
	eLegendName.appendChild(eFeatureNumber);
	
	var sourceAnnotationContainer = new YAHOO.util.Element(annotationContainerName);
	
	// Just for decipher
	// Todo: Include in sources a new atribute to define display true/false.
	// True = display anotations. False = loads annotations but do not display them
	if(annotationSource.id == "intl_clonesets"){
		sourceAnnotationContainer.setStyle('display','none');	
	}
	
	var sourceAnnotationContainerDisplay = sourceAnnotationContainer.getStyle('display');
	//console.log(sourceAnnotationContainerDisplay);
	
	var inputCheckbox = document.createElement("input");
	var eInputCheckbox = new YAHOO.util.Element(inputCheckbox);
	eInputCheckbox.set('type','checkbox');
	eInputCheckbox.set('id', legendContainerName + '_s' + sourceArrayNumber + '_icheckbox');
	if(sourceAnnotationContainerDisplay == 'block'){
		eInputCheckbox.set('checked',true);
	} else {
		eInputCheckbox.set('checked',false);
	}
	eInputCheckbox.setStyle('margin-right', '2px');
	eInputCheckbox.setStyle('position', 'relative');
	eInputCheckbox.setStyle('top', '0px');
	eInputCheckbox.setStyle('float', 'left');

	var sourceLegendContainer = document.createElement("div");
	var eSourceLegendContainer = new YAHOO.util.Element(sourceLegendContainer);
	eSourceLegendContainer.set('id', legendContainerName + '_s' + sourceArrayNumber);
	eSourceLegendContainer.setStyle('position', 'relative');
	eSourceLegendContainer.setStyle('margin-top', '0px');
	eSourceLegendContainer.setStyle('margin-bottom', '3px');
	eSourceLegendContainer.setStyle('margin-right', '14px');
	eSourceLegendContainer.setStyle('float', 'left');
	
	eSourceLegendContainer.appendChild(eInputCheckbox);
	//eSourceLegendContainer.appendChild(eLegendColor);
	eSourceLegendContainer.appendChild(eLegendName);
	
	var eChrLegend = new YAHOO.util.Element(legendContainerName);
	//eChrLegend.setStyle('margin-top', '7px');
	//eChrLegend.setStyle('margin-bottom', '7px');
	eChrLegend.appendChild(eSourceLegendContainer);
	//console.log(annotationSource.id);
	//console.log(annotationSource.name);
	//console.log(annotationSource.label);
	//console.log(annotationSource.description);
	
	YAHOO.util.Event.addListener(legendContainerName + '_s' + sourceArrayNumber + '_icheckbox', "click", annotationSourceCheckboxCallback, annotationContainerName);

	var sourceView = new Object();
	sourceView.selectedChromosome = selectedChromosome;
	sourceView.viewPrefix = viewPrefix;
	sourceView.annotationSource = annotationSource;
	YAHOO.util.Event.addListener(legendContainerName + '_s' + sourceArrayNumber + '_name', "click", annotationSourceClickCallback, sourceView);
	
	//YAHOO.util.Event.addListener(legendContainerName + '_s' + sourceArrayNumber + '_name', "mouseover", annotationSourceMouseoverCallback, annotationSource);
	//YAHOO.util.Event.addListener(legendContainerName + '_s' + sourceArrayNumber + '_name', "mouseout", annotationSourceMouseoutCallback, annotationSource);
}


function annotationSourceCheckboxCallback(e, container) {
	var eAnnotationContainer = new YAHOO.util.Element(container);
	if(this.checked){
		eAnnotationContainer.setStyle('visible', 'true');
		eAnnotationContainer.setStyle('display', 'block');
	} else {
		eAnnotationContainer.setStyle('visible', 'false');
		eAnnotationContainer.setStyle('display', 'none');
	}
	var start = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.start;
	var stop = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.stop;
	var selectedChromosome = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.selectedChromosome;
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.spotZoomRegion(selectedChromosome, start, stop);
}



function annotationSourceClickCallback(e, sourceView){
	var eContainer = new YAHOO.util.Element(this.parentNode.id);
	var featurePosition = YAHOO.util.Dom.getXY(this.id); 
	var toolTipIdName = this.id+'_tt1';
	var toolTipPosition = YAHOO.util.Dom.getXY(toolTipIdName);

	if(toolTipPosition){
		YAHOO.dasBrowser.yuiContainer.panel[toolTipIdName].show();
	} else {
		if (sourceView.viewPrefix == YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.labelPrefix) {
			var selectedBand = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.selectedBand;
			var selectedSequenceStart = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[sourceView.selectedChromosome].bands[selectedBand].start;
			var selectedSequenceEnd = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[sourceView.selectedChromosome].bands[selectedBand].end;
		} else if (sourceView.viewPrefix == YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix) {
			var selectedSequenceStart = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.start;
			var selectedSequenceEnd = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.stop;
		} else {
			var selectedSequenceStart = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[sourceView.selectedChromosome].start;
			var selectedSequenceEnd = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[sourceView.selectedChromosome].stop;
		}
		
		var tooltipPanel = document.createElement("div");
		var eTooltipPanel = new YAHOO.util.Element(tooltipPanel);
		eTooltipPanel.set('id', toolTipIdName);
		
		var tooltipHead = document.createElement("div");
		var eTooltipHead = new YAHOO.util.Element(tooltipHead);
		eTooltipHead.addClass('hd');
		sourceView.annotationSource.id = 'Selected';
		eTooltipHead.appendChild(document.createTextNode(sourceView.annotationSource.name + " (" + sourceView.annotationSource.id + ")"));
		
		var tooltipBody = document.createElement("div");
		var eTooltipBody = new YAHOO.util.Element(tooltipBody);
		eTooltipBody.addClass('bd');
		eTooltipBody.appendChild(document.createTextNode(sourceView.annotationSource.description));

		var tooltipFoot = document.createElement("div");
		var eTooltipFoot = new YAHOO.util.Element(tooltipFoot);
		eTooltipFoot.addClass('ft');
		
		var listAnnotationInfo = new Object();
		listAnnotationInfo.selectedChromosome = sourceView.selectedChromosome;
		listAnnotationInfo.selectedSource = sourceView.annotationSource;
		listAnnotationInfo.selectedSequenceStart = selectedSequenceStart;
		listAnnotationInfo.selectedSequenceEnd = selectedSequenceEnd;
		listAnnotationInfo.viewPrefix = sourceView.viewPrefix;

		
		//var listAnnotLink = document.createElement("a");
		//var eListAnnotLink = new YAHOO.util.Element(listAnnotLink);
		//eListAnnotLink.appendChild(document.createTextNode("Display list of annotations"));
		//eListAnnotLink.set('href', "#");
		//eListAnnotLink.addListener('click', listAnnotationLinkClickCallback, listAnnotationInfo);
		//eTooltipFoot.appendChild(eListAnnotLink);
		//eTooltipFoot.appendChild(document.createElement("br"));
		
		var chromosomeNumber = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[sourceView.selectedChromosome].id;
		var annotationSourceEnsemblUrl = sourceView.annotationSource.url.split("/features")
		var selectedEnsemblCoordinates = ':' + selectedSequenceStart + '-' + selectedSequenceEnd;
		var linkToEnsembl = document.createElement("a");
		var eLinkToEnsembl = new YAHOO.util.Element(linkToEnsembl);
		eLinkToEnsembl.appendChild(document.createTextNode("Display annotations in Ensembl"));
		eLinkToEnsembl.set('href', "http://www.ensembl.org/Homo_sapiens/Location/View?r=" + chromosomeNumber + selectedEnsemblCoordinates + ";contigviewbottom=das:" + annotationSourceEnsemblUrl[0]);
		eLinkToEnsembl.set('target', '_ensembl');
		eTooltipFoot.appendChild(eLinkToEnsembl);
		eTooltipFoot.appendChild(document.createElement("br"));
			
		
		
		var selectedCoordinates = ':' + selectedSequenceStart + ',' + selectedSequenceEnd;
		var originalSourceLink = document.createElement("a");
		var eOriginalSourceLink = new YAHOO.util.Element(originalSourceLink);
		eOriginalSourceLink.set('href', YAHOO.dasBrowser.genome.karyotypeStructure.proxyUrl + "?" + YAHOO.dasBrowser.proxy.parameters.source + "=" + sourceView.annotationSource.url + '?segment=' + chromosomeNumber + selectedCoordinates);
		eOriginalSourceLink.set('target', '_dasSource');
		eOriginalSourceLink.appendChild(document.createTextNode("View Original Data Source"));
		eTooltipFoot.appendChild(eOriginalSourceLink);
		
		var sourceId = sourceView.annotationSource.id.toUpperCase();
		if(sourceId.indexOf("DS_")!= -1){
			var dasRegistryLink = document.createElement("a");
			var eDasRegistryLink = new YAHOO.util.Element(dasRegistryLink);
			eDasRegistryLink.set('href', 'http://www.dasregistry.org/showdetails.jsp?auto_id=' + sourceId);
			eDasRegistryLink.set('target', '_dasRegistry');
			//eDasRegistryLink.appendChild(document.createTextNode("Access DAS Registry information"));
			//eTooltipFoot.appendChild(document.createElement("br"));
			//eTooltipFoot.appendChild(eDasRegistryLink);
		}
	
		eTooltipPanel.appendChild(eTooltipHead);
		eTooltipPanel.appendChild(eTooltipBody);
		if(sourceView.annotationSource.id !="External source"){
			eTooltipPanel.appendChild(eTooltipFoot);
		}
		eContainer.appendChild(eTooltipPanel);
		
		YAHOO.dasBrowser.yuiContainer.panel[toolTipIdName] = new YAHOO.widget.Panel(toolTipIdName, {xy:featurePosition, width:"300px", zIndex:3});
		YAHOO.dasBrowser.yuiContainer.panel[toolTipIdName].render();
	}
	//console.log(this.parentNode.id);
}


function listAnnotationLinkClickCallback(e, listAnnotationInfo){
	// WIPE PREVIOUS CHROMOSOME CONTENT
	var container = document.getElementById('annotationListView');
	container.innerHTML = "";
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawAnnotationListViewStructure(listAnnotationInfo.selectedChromosome, listAnnotationInfo.selectedSource, listAnnotationInfo.selectedSequenceStart, listAnnotationInfo.selectedSequenceEnd, listAnnotationInfo.viewPrefix);
	// OPEN ANNOTATION LIST VIEW
	YAHOO.dasBrowser.yuiContainer.panel['annotationListView'].show();
}
/*
 * 
 * <div id=\"BvContLegend_chromosome1_s0_colorrafa\" style=\"border-top: 1px solid rgb(0, 0, 0); border-bottom: 1px solid rgb(0, 0, 0); height: 7px; width: 9px; margin-top: 1px; margin-right: 3px; background-color: rgb(204, 0, 0); float: left;\"/>
 
id = 'DS_224';
source03.url ='http://das.ensembl.org/das/toronto_poly/features';
source03.name ='toronto poly';
source03.label =['ENSEMBL'];
source03.description 
*/

/*
 * REDRAW karyotype componets * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 */

/*
 * REDRAW full band
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.redrawBand = function(selectedChromosome, selectedBand){
	
	// SELECTED CHROMOSOME
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.selectedChromosome = selectedChromosome;
	// SELECTED BAND
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.selectedBand = selectedBand;
	// WIPE PREVIOUS BAND CONTENT
	var container = document.getElementById('bandView');
	container.innerHTML = "";
	// DRAW STRUCTURE VIEW FOR THE NEW BAND CONTENT
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawBandViewStructure(
		selectedChromosome,
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.layout
	);
	//DRAW SLIDER
	var start = parseInt(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosome].bands[selectedBand].start);
	var stop = parseInt(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosome].bands[selectedBand].end);
	var pxWidth = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.chromosomeWidthPx;
	var bandSlider = new mySlider("BvSliderFrame_" + selectedChromosome, "BvSlider_" + selectedChromosome);
	bandSlider.setHtml();
	bandSlider.setRange(pxWidth);
	bandSlider.setBgImg("img/bg-slider.png", pxWidth+"px")
	bandSlider.setSeqStart(start);
	bandSlider.setSeqStop(stop);
	bandSlider.setSlider();
	
	
	if (YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.showBand) {
		// DRAW BAND
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawBand(
			YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.selectedChromosome,
			YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.labelPrefix,
			YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.bandWidthPx,
			YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.bandHeightPx
		);	
	}
	// DRAW ANNOTATIONS
	var karyotypeMaxbins = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.annotationLimit; // 300
	var chromosomeMaxbins = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.annotationLimit; //1500
	var bandMaxbins = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.annotationLimit; //1500
	var annotationSources = YAHOO.dasBrowser.genome.annotations.sources;
	for (var j = 0; j < annotationSources.length; j++) {
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.featureNumber = 0;
		var positionalFeatures = 0;
		if (YAHOO.lang.isUndefined(annotationSources[j].result[selectedChromosome])) {
			//console.log("Warning ... the source: \"" + annotationSources[j].id + "\" is not displaying results for " + selectedChromosome + " on the band view");
		} else {
			if (annotationSources[j].ntLimit == null || annotationSources[j].ntLimit > (stop - start)) {
				var sourceArrayNumber = j;
				
				//Set annotation types prefix
				var annotationTypesSelected = YAHOO.dasBrowser.genome.annotations.sources[j].types;
				var annotationTypesSufix = "";
				if (annotationTypesSelected.length > 0) {
					for (var k = 0; k < annotationTypesSelected.length; k++) {
						annotationTypesSufix = annotationTypesSufix + ";type=" + annotationTypesSelected[k];
					}
				}
				
				//if (sourceArrayNumber < YAHOO.dasBrowser.genome.annotations.simpleView.maxAnnotationSources) {
				if (YAHOO.dasBrowser.genome.annotations.showSimpleView && YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.showBand) {
					var makeStraightRequest = false;
					if (annotationSources[j].panel == "karyotype") {
						if(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment == null){
							var segmentFeatureNumber = 0;
						} else {
							var segmentFeatureNumber = YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment.featureNumber; // ~560
						}
					}
					else 
						if (annotationSources[j].panel == "chromosome") {
							var segmentFeatureNumber = YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment.featureNumber; // ~560					
							if(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment == null){
								var segmentFeatureNumber = 0;
							} else {
								var segmentFeatureNumber = YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment.featureNumber; // ~560
							}
						}
						else 
							if (annotationSources[j].panel == "zoom" || annotationSources[j].panel == "band") {
								makeStraightRequest = true;
								var segmentFeatureNumber = 0;
							}
					if (segmentFeatureNumber > karyotypeMaxbins || makeStraightRequest) {
						if (segmentFeatureNumber > chromosomeMaxbins || makeStraightRequest) {
							// Make Band annotation Request per source per segment.
							var selectedBand = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.selectedBand;
							var sourceUrl = YAHOO.dasBrowser.genome.annotations.sources[j].url + "?segment=";
							var segmentId = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].id;
							//var segmentStart = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].start;
							//var segmentStop = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].stop;
							var referenceStartSeq = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosome].bands[selectedBand].start;
							var referenceEndSeq = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosome].bands[selectedBand].end;
							var annotationUrl = YAHOO.dasBrowser.genome.karyotypeStructure.proxyUrl + "?" + YAHOO.dasBrowser.proxy.parameters.source + "=" + escape(sourceUrl + segmentId + ":" + referenceStartSeq + "," + referenceEndSeq + ";maxbins=" + bandMaxbins + annotationTypesSufix);
							var request = YAHOO.util.Connect.asyncRequest('GET', annotationUrl, annotationCallback);
							YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId] = new Object;
							YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].source = [j];
							YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].chromosome = selectedChromosome;
							YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].view = "Bv";
							YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].redraw = true;
						}
						else {
							YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawAnnotations(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.features, YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment, null, selectedChromosome, YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.labelPrefix, sourceArrayNumber);
							//console.log("f:"+ YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment.positional);
							YAHOO.dasBrowser.genome.annotations.simpleView.drawLegend(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.labelPrefix, selectedChromosome, sourceArrayNumber, YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment.positional);
							//console(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment.positional);
						}
						
					}
					else {
						if (annotationSources[j].panel == "karyotype") {
							if (YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment != null) {
								YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawAnnotations(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.features, YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment, null, selectedChromosome, YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.labelPrefix, sourceArrayNumber);
								positionalFeatures = YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment.positional;
							}
							YAHOO.dasBrowser.genome.annotations.simpleView.drawLegend(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.labelPrefix, selectedChromosome, sourceArrayNumber, positionalFeatures);
						}
						else {
							if (YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment != null) {
								YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawAnnotations(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.features, YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment, null, selectedChromosome, YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.labelPrefix, sourceArrayNumber);
								positionalFeatures = YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment.positional;
							}
							YAHOO.dasBrowser.genome.annotations.simpleView.drawLegend(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.labelPrefix, selectedChromosome, sourceArrayNumber, positionalFeatures);
						}
					}
				}
			//}
			}
		}
	}
	// HIDE ANNOTATION LIST VIEW
	YAHOO.dasBrowser.yuiContainer.panel['annotationListView'].hide();
}

/*
 * REDRAW full chromosome
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.redrawChromosome = function(selectedChromosome){
	var karyotypeMaxbins = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.annotationLimit; // 300
	var chromosomeMaxbins = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.annotationLimit; //1500
	//var bandMaxbins = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.annotationLimit; //1500
	var isNullKaryotypeFeatures = YAHOO.lang.isNull(YAHOO.dasBrowser.genome.karyotypeStructure.dsn.selectedDsn.karyotypeLocation);
	// SELECT CHROMOSOME
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.selectedChromosome = selectedChromosome;
	
	// WIPE PREVIOUS CHROMOSOME CONTENT
	var container = document.getElementById('chromosomeView');
	container.innerHTML = "";
	// DRAW STRUCTURE VIEW FOR THE NEW CHROMOSOME CONTENT
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawChromosomeViewStructure(
		selectedChromosome,
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.layout
	);
	//DRAW SLIDER
	var pxWidth = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.chromosomeWidthPx;
	var chromosomeSlider = new mySlider("CvSliderFrame_" + YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.selectedChromosome, "CvSlider_" + YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.selectedChromosome);
	chromosomeSlider.setHtml();
	chromosomeSlider.setRange(pxWidth);
	chromosomeSlider.setBgImg("img/bg-slider.png", pxWidth+"px")
	chromosomeSlider.setSeqStart(parseInt(YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].start));
	chromosomeSlider.setSeqStop(parseInt(YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].stop));
	chromosomeSlider.setSlider();
	
	// DRAW CHROMOSOME
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawChromosome(
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.selectedChromosome,
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.labelPrefix,
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.chromosomeWidthPx,
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.chromosomeHeightPx
	);
	
	// DRAW CHROMOSOME BANDS
	if(isNullKaryotypeFeatures == false){	
		if(YAHOO.lang.isUndefined(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosome].bands)==false){
			YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawChromosomeBands(
				YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.selectedChromosome,
				YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.labelPrefix,
				YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.chromosomeWidthPx,
				YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.chromosomeHeightPx
			);
		}
	}

	// DRAW ANNOTATIONS
	var annotationSources = YAHOO.dasBrowser.genome.annotations.sources;
	for (var j = 0; j < annotationSources.length; j++) {
		var sourceArrayNumber = j;
		
		//Set annotation types prefix
		var annotationTypesSelected = YAHOO.dasBrowser.genome.annotations.sources[j].types;
		var annotationTypesSufix = "";
		if(annotationTypesSelected.length > 0){
			for (var k = 0; k < annotationTypesSelected.length; k++) {
				annotationTypesSufix = annotationTypesSufix + ";type=" + annotationTypesSelected[k];
			}
		}
		//if (sourceArrayNumber < YAHOO.dasBrowser.genome.annotations.simpleView.maxAnnotationSources) {
			if (YAHOO.dasBrowser.genome.annotations.showSimpleView && YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.showChromosome) {
				
				if(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment ==null){
					makeAnnotationCvRequest = true;
				} else {
					var kvSegmentFeatureNumber = YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment.featureNumber; // ~560
					var makeAnnotationCvRequest = false;
					if (kvSegmentFeatureNumber > karyotypeMaxbins) {
						makeAnnotationCvRequest = true;
						if(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment != null){
							var makeAnnotationCvRequest = false;
						}
					}
				}
					
				if (makeAnnotationCvRequest){
					// Make Chromsome annotation Request per source per segment.
					if (annotationSources[j].panel == "karyotype" || annotationSources[j].panel == "chromosome") {
						var sourceUrl = YAHOO.dasBrowser.genome.annotations.sources[j].url + "?segment=";
						var segmentId = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].id;
						var segmentStart = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].start;
						var segmentStop = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].stop;
						var annotationUrl = YAHOO.dasBrowser.genome.karyotypeStructure.proxyUrl + "?" + YAHOO.dasBrowser.proxy.parameters.source + "=" + sourceUrl + segmentId + ":" + segmentStart + "," + segmentStop + ";maxbins=" + chromosomeMaxbins + annotationTypesSufix;
						var request = YAHOO.util.Connect.asyncRequest('GET', annotationUrl, annotationCallback);
						YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId] = new Object;
						YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].source = [j];
						YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].chromosome = selectedChromosome;
						YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].view = "Cv";
						YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].redraw = true;
					}
				}else {
					YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawAnnotations(
						YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.features,
						YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment,
						null,
						selectedChromosome,
						YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.labelPrefix,
						sourceArrayNumber
					);
					//console.log("c:"+YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment.positional); 
					YAHOO.dasBrowser.genome.annotations.simpleView.drawLegend(
						YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.labelPrefix,
						selectedChromosome,
						sourceArrayNumber,
						YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment.positional
					);
					
				}
			}
		//}
	}

	if(isNullKaryotypeFeatures == false){
		// SET SELECTED BAND TO THE FIRST VALUE
		var selectedBand = 0;
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.selectedBand = selectedBand;
		
		// WIPE PREVIOUS BAND CONTENT
		//var container = document.getElementById('bandView');
		//container.innerHTML = "";
		
		// DRAW SELECTED BAND
		//YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.redrawBand(selectedChromosome, selectedBand);
		
		// HIDE BAND VIEW
		YAHOO.dasBrowser.yuiContainer.panel['bandView'].hide();
		
		// HIDE ZOOM VIEW
		YAHOO.dasBrowser.yuiContainer.panel['zoomView'].hide();
	}
	// HIDE ANNOTATION LIST VIEW
	YAHOO.dasBrowser.yuiContainer.panel['annotationListView'].hide();
}



//DRAW spot Zoom region 
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.spotZoomRegion = function(selectedChromosome, start, stop){
	var viewContainerPrefix = new Array();
	viewContainerPrefix.push(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.labelPrefix);
	viewContainerPrefix.push(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.labelPrefix);
	var numberOfZoomMarkSources = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.numberOfZoomMarkSources;
	
	for (var j = 0; j < viewContainerPrefix.length; j++) {
		var chromosomeContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.labelPrefix;
		var chromosomeRepresentationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.chromosomeRepresentationContainer.labelPrefix;
		var spotZoomContainer_id = viewContainerPrefix[j] + chromosomeContainerPrefix + "SpotZoom_" + selectedChromosome;
		var chrContainer_id = viewContainerPrefix[j] + chromosomeContainerPrefix + "_" + selectedChromosome;
		var chrDivContainer_id = viewContainerPrefix[j] + "Details_" + selectedChromosome;
		var repMarginLeft = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.leftPaddingPx;
		var zoomStart = start;
		var zoomStop = stop;
		var bandExtraMargin = 4;
		var chrExtraMargin = 3;
		var zoomDisplay = true;

		if (document.getElementById(chrDivContainer_id)) {
			var chrDivContainer = document.getElementById(chrDivContainer_id);
			var chrWidthPx = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.chromosomeWidthPx;
			
			if(viewContainerPrefix[j] == YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.labelPrefix){
				var selectedBand = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.selectedBand;
				var referenceStartSeq = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosome].bands[selectedBand].start;
				var referenceEndSeq = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosome].bands[selectedBand].end;
				if(stop > referenceEndSeq){
					zoomStop = referenceEndSeq;
				}
				if(start < referenceStartSeq){
					zoomStart = referenceStartSeq;
				}
				
				if(referenceEndSeq < start){
					zoomDisplay = false;
				} else if(referenceStartSeq > stop) {
					zoomDisplay = false;
				}
			} else {
				var entryPoints = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome];
				var referenceStartSeq = entryPoints.start;
				var referenceEndSeq = entryPoints.stop;
			}
			
			if (zoomDisplay == true) {
				var zoomCoordinates = new myFeaturePosition();
				zoomCoordinates.setReferenceLengthPx(chrWidthPx);
				zoomCoordinates.setFeatureStartSeq(zoomStart);
				zoomCoordinates.setFeatureEndSeq(zoomStop);
				zoomCoordinates.setReferenceStartSeq(referenceStartSeq);
				zoomCoordinates.setReferenceEndSeq(referenceEndSeq);
				zoomCoordinates.calculatePx();
				
				var spotZoomContainer = document.createElement("div");
				var eSpotZoomContainer = new YAHOO.util.Element(spotZoomContainer);
				eSpotZoomContainer.set('id', spotZoomContainer_id);
				//eSpotZoomContainer.setStyle('background-color', '#f2f2f2');
				//eSpotZoomContainer.setStyle('width', widthPx);
				//eSpotZoomContainer.setStyle('height', heightPx);
				eSpotZoomContainer.setStyle('width', YAHOO.dasBrowser.util.addPxSuffix(zoomCoordinates.featureLengthPx));
				//eSpotZoomContainer.setStyle('height', heightPx);
				eSpotZoomContainer.setStyle('border', '1px dotted #cc0000');
				eSpotZoomContainer.setStyle('position', 'absolute');
				eSpotZoomContainer.setStyle('top', '-1px');
				eSpotZoomContainer.setStyle('left', YAHOO.dasBrowser.util.addPxSuffix(zoomCoordinates.featureStartPx));
				eSpotZoomContainer.setStyle('margin-left', YAHOO.dasBrowser.util.addPxSuffix(repMarginLeft));
				eSpotZoomContainer.setStyle('z-index', '1');
				//eSpotZoomContainer.setStyle('cursor', 'pointer');
				
				var eChrContainer = new YAHOO.util.Element(chrContainer_id);
				var eSpotZoomContainerHeight = eChrContainer.getStyle('height');
				if (viewContainerPrefix[j] == YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.labelPrefix) {
					var eSpotZoomContainerHeightCorrect = YAHOO.dasBrowser.util.deletePxSuffix(eSpotZoomContainerHeight) + (bandExtraMargin*numberOfZoomMarkSources);
				}
				else {
					var eSpotZoomContainerHeightCorrect = YAHOO.dasBrowser.util.deletePxSuffix(eSpotZoomContainerHeight) + chrExtraMargin;
				}
				eSpotZoomContainer.setStyle('height', YAHOO.dasBrowser.util.addPxSuffix(eSpotZoomContainerHeightCorrect));
				
				// Delete previus ZOOM regions
				chrDivContainer.innerHTML = "";
				
				// Add new ZOOM region
				var eChrDivContainer = new YAHOO.util.Element(chrDivContainer);
				eChrDivContainer.appendChild(eSpotZoomContainer);
			}
		}
	}
}

/*
 * REDRAW full ZOOM region
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.redrawZoom = function(selectedChromosome, start, stop){
	var karyotypeMaxbins = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.annotationLimit; 
	var zoomMaxbins = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.annotationLimit;
	var chromosomeMaxbins = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.annotationLimit;
	var isNullKaryotypeFeatures = YAHOO.lang.isNull(YAHOO.dasBrowser.genome.karyotypeStructure.dsn.selectedDsn.karyotypeLocation);
	
	// SELECT CHROMOSOME
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.selectedChromosome = selectedChromosome;
	
	//ZOOM VALUES
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.start = start;
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.stop = stop;
	
	// WIPE PREVIOUS ZOOM CONTENT
	var container = document.getElementById('zoomView');
	container.innerHTML = "";
	// DRAW STRUCTURE VIEW FOR THE NEW CHROMOSOME CONTENT //selectedChromosome, start, end, layout
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawZoomViewStructure(
		selectedChromosome,
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.layout
	);

	

	// DRAW CHROMOSOME
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawZoom(
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.selectedChromosome,
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix,
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.widthPx,
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.heightPx
	);
	if(isNullKaryotypeFeatures == false){	
		// DRAW CHROMOSOME BANDS
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawChromosomeBands(
			YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.selectedChromosome,
			YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix,
			YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.widthPx,
			YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.heightPx
		);
		var chromosomeBands = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosome].bands;
	}
	
	// DRAW ANNOTATIONS
	var annotationSources = YAHOO.dasBrowser.genome.annotations.sources;
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.numberOfZoomMarkSources = 0;
	for (var j = 0; j < annotationSources.length; j++) {
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.featureNumber = 0;
		//console.log(annotationSources[j].id);
		if (YAHOO.lang.isUndefined(annotationSources[j].result[selectedChromosome])) {
			//console.log("Warning ... the source: \"" + annotationSources[j].id + "\" is not displaying results for " + selectedChromosome + " on the zoom view");
		} else {
			//console.log(annotationSources[j].id);
			if (annotationSources[j].ntLimit == null || annotationSources[j].ntLimit > (stop - start)) {
				if (annotationSources[j].representation == 'mark' || annotationSources[j].representation == 'track') {
					YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.numberOfZoomMarkSources++;
				}
				var sourceArrayNumber = j;
				//Set annotation types prefix
				var annotationTypesSelected = YAHOO.dasBrowser.genome.annotations.sources[j].types;
				var annotationTypesSufix = "";
				if (annotationTypesSelected.length > 0) {
					for (var k = 0; k < annotationTypesSelected.length; k++) {
						annotationTypesSufix = annotationTypesSufix + ";type=" + annotationTypesSelected[k];
					}
				}
				//if (sourceArrayNumber < YAHOO.dasBrowser.genome.annotations.simpleView.maxAnnotationSources) {
				if (YAHOO.dasBrowser.genome.annotations.showSimpleView && YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.showZoom) {
					var makeStraightRequest = false;
					if (annotationSources[j].panel == "karyotype") {
						if(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment == null){
							var segmentFeatureNumber = 0;
						} else {
							var segmentFeatureNumber = YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment.featureNumber; // ~560
						}
					} else if (annotationSources[j].panel == "chromosome") {
						if(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment == null){
							var segmentFeatureNumber = 0;
						} else {
							var segmentFeatureNumber = YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment.featureNumber; // ~560
						}
					} else if (annotationSources[j].panel == "zoom" || annotationSources[j].panel == "band") {
						makeStraightRequest = true;
						var segmentFeatureNumber = 0;
					}

					if (segmentFeatureNumber > karyotypeMaxbins || makeStraightRequest) {
						if (segmentFeatureNumber > chromosomeMaxbins || makeStraightRequest) {
							// Make Chromsome annotation Request per source per segment.
							var sourceUrl = YAHOO.dasBrowser.genome.annotations.sources[j].url + "?segment=";
							var segmentId = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].id;
							var segmentStart = parseInt(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.start);
							var segmentStop = parseInt(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.stop);
							var annotationUrl = YAHOO.dasBrowser.genome.karyotypeStructure.proxyUrl + "?" + YAHOO.dasBrowser.proxy.parameters.source + "=" + sourceUrl + segmentId + ":" + segmentStart + "," + segmentStop + ";maxbins=" + zoomMaxbins + annotationTypesSufix;
							var request = YAHOO.util.Connect.asyncRequest('GET', annotationUrl, annotationCallback);
							YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId] = new Object;
							YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].source = [j];
							YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].chromosome = selectedChromosome;
							YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].view = "Zv";
							YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].redraw = true;
						}
						else {
							YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawAnnotations(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.features, YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment, null, selectedChromosome, YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix, sourceArrayNumber);
							//console.log("a:"+positionalFeatures);
							YAHOO.dasBrowser.genome.annotations.simpleView.drawLegend(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix, selectedChromosome, sourceArrayNumber, YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment.positional);
							
						}
						
					}
					else {
						var positionalFeatures = 0;
						if (annotationSources[j].panel == "karyotype") {
							if (YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment != null) {
								YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawAnnotations(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.features, YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment, null, selectedChromosome, YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix, sourceArrayNumber);
								positionalFeatures = YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment.positional;
							}
							//console.log("b1:"+positionalFeatures);
							YAHOO.dasBrowser.genome.annotations.simpleView.drawLegend(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix, selectedChromosome, sourceArrayNumber, positionalFeatures);
						}
						else {
							if (YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment != null) {
								YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawAnnotations(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.features, YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment, null, selectedChromosome, YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix, sourceArrayNumber);
								positionalFeatures = YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment.positional;
							}
							//console.log("b2:"+positionalFeatures);
							YAHOO.dasBrowser.genome.annotations.simpleView.drawLegend(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix, selectedChromosome, sourceArrayNumber, positionalFeatures);
						}
						
						
					}
					
				/*
				 var kvSegmentFeatureNumber = YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment.featureNumber; // ~560
				 var makeAnnotationCvRequest = false;
				 if (kvSegmentFeatureNumber > chromosomeMaxbins) {
				 makeAnnotationCvRequest = true;
				 if(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].cv.segment != null){
				 var makeAnnotationCvRequest = false;
				 }
				 }
				 if (makeAnnotationCvRequest){
				 // Make Chromsome annotation Request per source per segment.
				 var sourceUrl = YAHOO.dasBrowser.genome.annotations.sources[j].url + "?segment=";
				 var segmentId = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].id;
				 var segmentStart = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].start;
				 var segmentStop = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosome].stop;
				 var annotationUrl = YAHOO.dasBrowser.genome.karyotypeStructure.proxyUrl + "?" + YAHOO.dasBrowser.proxy.parameters.source + "=" + sourceUrl + segmentId + ":" + segmentStart + "," + segmentStop + ";maxbins=" + zoomMaxbins + annotationTypesSufix;
				 var request = YAHOO.util.Connect.asyncRequest('GET', annotationUrl, annotationCallback);
				 YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId] = new Object;
				 YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].source = [j];
				 YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].chromosome = selectedChromosome;
				 YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].view = "Zv";
				 YAHOO.dasBrowser.genome.annotations.mappingRequestIdSource[request.tId].redraw = true;
				 }else {
				 YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawAnnotations(
				 YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.features,
				 YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment,
				 null,
				 selectedChromosome,
				 YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix,
				 sourceArrayNumber
				 );
				 
				 YAHOO.dasBrowser.genome.annotations.simpleView.drawLegend(
				 YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix,
				 selectedChromosome,
				 sourceArrayNumber,
				 YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].result[selectedChromosome].kv.segment.positional
				 );
				 
				 }
				 */
				}
			//}
			}
		}
	}
	
	// DRAW SPOT ZOOM REGION
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.spotZoomRegion(selectedChromosome, start, stop);
	
	if(isNullKaryotypeFeatures == false){
		/*
		// SET SELECTED BAND TO THE FIRST VALUE
		var selectedBand = 0;
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.bandConf.selectedBand = selectedBand;
		
		// WIPE PREVIOUS BAND CONTENT
		var container = document.getElementById('bandView');
		container.innerHTML = "";
		
		// DRAW SELECTED BAND
		//YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.redrawBand(selectedChromosome, selectedBand);
		
		// HIDE BAND VIEW
		YAHOO.dasBrowser.yuiContainer.panel['bandView'].hide();
		*/
		// SHOW ZOOM VIEW
		YAHOO.dasBrowser.yuiContainer.panel['zoomView'].show();
	}
	// HIDE ANNOTATION LIST VIEW
	YAHOO.dasBrowser.yuiContainer.panel['annotationListView'].hide();
}



/*
 * Set CCS styles * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 */

/*
 * VIEW titles
 */
YAHOO.dasBrowser.genome.panel.setPanelTitleStyle = function(divId){
	var divId = divId + 'Style';
	var panelTitleFontSizePx = YAHOO.dasBrowser.genome.panel.title.fontSizePx;
	var panelTitleFontWeight = YAHOO.dasBrowser.genome.panel.title.fontWeight;
	var panelTitleBorderBottom= YAHOO.dasBrowser.genome.panel.title.borderBottom;
	var panelTitleBackgroundColor = YAHOO.dasBrowser.genome.panel.title.backgroundColor;
	var panelTitleMarginBottom = YAHOO.dasBrowser.genome.panel.title.marginBottom;
	var panelTitleMarginTop = YAHOO.dasBrowser.genome.panel.title.marginTop;
	var panelTitlePadding = YAHOO.dasBrowser.genome.panel.title.padding;
	var panelFontColor = YAHOO.dasBrowser.genome.panel.title.fontColor;
	
	var ePanelTitle = new YAHOO.util.Element(divId);
	ePanelTitle.addClass('fontSize' + panelTitleFontSizePx);
	ePanelTitle.setStyle('font-weight', panelTitleFontWeight);
	ePanelTitle.setStyle('border-bottom', panelTitleBorderBottom);
	ePanelTitle.setStyle('background-color', panelTitleBackgroundColor);
	ePanelTitle.setStyle('margin-bottom', YAHOO.dasBrowser.util.addPxSuffix(panelTitleMarginBottom));
	ePanelTitle.setStyle('margin-top', YAHOO.dasBrowser.util.addPxSuffix(panelTitleMarginTop));
	ePanelTitle.setStyle('padding', YAHOO.dasBrowser.util.addPxSuffix(panelTitlePadding));
	ePanelTitle.setStyle('color', panelFontColor);
	ePanelTitle.setStyle('clear', 'both');
}


/*
 * Karyotype listeners (events)  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 */

/*
 * REDRAW band Listener
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.addRedrawBandListener = function(chrId, bandId, panel){
	if (panel == "chromosome") {
		var viewContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.labelPrefix;
	} else if(panel == "zoom"){
		var viewContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix;
	}
	var chromosomeContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.labelPrefix;
	var chromosomeRepresentationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.chromosomeRepresentationContainer.labelPrefix;
	var chromosomeBandContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.chromosomeRepresentationContainer.chromosomeBandContainer.labelPrefix;
	var bandIdName = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[chrId].bands[bandId].id;
	var divContainer = viewContainerPrefix + chromosomeContainerPrefix + chromosomeRepresentationContainerPrefix + chromosomeBandContainerPrefix + "_" + chrId + "_" + bandIdName;
	//console.log(panel +": "+divContainer);
	YAHOO.util.Event.addListener(divContainer, "click", YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.redrawBandCallback, [chrId, bandId]); //bandId
}

YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.redrawBandCallback = function(e, idArray){
	//YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.redrawBand(idArray[0], idArray[1]);
	// OPEN BAND VIEW
	//YAHOO.dasBrowser.yuiContainer.panel['bandView'].show();
	
	// OPEN ZOOM VIEW
	var start = parseInt(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[idArray[0]].bands[idArray[1]].start);
	var stop = parseInt(YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[idArray[0]].bands[idArray[1]].end);
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.redrawZoom(idArray[0], start, stop);
	YAHOO.dasBrowser.yuiContainer.panel['bandView'].show();
} 


/*
 * REDRAW zoom Listener
 */

/*
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.addRedrawZoomListener = function(chrId, bandId){
	var viewContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.zoomConf.labelPrefix;
	var chromosomeContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.labelPrefix;
	var chromosomeRepresentationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.chromosomeRepresentationContainer.labelPrefix;
	var chromosomeBandContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.chromosomeRepresentationContainer.chromosomeBandContainer.labelPrefix;
	var bandIdName = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[chrId].bands[bandId].id;
	var divContainer = viewContainerPrefix +  chromosomeContainerPrefix + chromosomeRepresentationContainerPrefix + chromosomeBandContainerPrefix + "_" + chrId + "_" + bandIdName;
	YAHOO.util.Event.addListener(divContainer, "click", YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.redrawBandCallback, [chrId, bandId]); //bandId
}
*/

/*
 * REDRAW chromosome Listener
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.addRedrawChromosomeListener = function(chrId){
	var viewContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.labelPrefix;
	var chromosomeContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.labelPrefix;
	var chromosomeRepresentationContainerPrefix = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.panelContainer.chromosomeContainer.chromosomeRepresentationContainer.labelPrefix;
	//CvContChrRep_chromosome1
	var divContainer = viewContainerPrefix +  chromosomeContainerPrefix + chromosomeRepresentationContainerPrefix + "_" + chrId;
	YAHOO.util.Event.addListener(divContainer, "click", YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.redrawChromosomeCallback, chrId);
}

YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.redrawChromosomeCallback = function(e, chrId) {
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.redrawChromosome(chrId);
	var chromosomeBands = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[chrId].bands;
	if (!YAHOO.lang.isUndefined(chromosomeBands)) {
		var isNullKaryotypeFeatures = YAHOO.lang.isNull(YAHOO.dasBrowser.genome.karyotypeStructure.dsn.selectedDsn.karyotypeLocation);
		if (isNullKaryotypeFeatures == false) {
			for (var j = 0; j < chromosomeBands.length; j++) {
				YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.addRedrawBandListener(chrId, j, "chomosome");
			}
		}
	}
} 

/*
 * Panel Listener
 */
YAHOO.dasBrowser.genome.panel.setPanelListeners = function(viewName, isVisible){
	//var container = new YAHOO.util.Element(viewName);
	//container.setStyle('position', 'relative');
	YAHOO.dasBrowser.yuiContainer.panel[viewName] = new YAHOO.widget.Module(viewName, { visible: isVisible });
	YAHOO.dasBrowser.yuiContainer.panel[viewName].render();
	YAHOO.util.Event.addListener(viewName+"Display", "click", YAHOO.dasBrowser.genome.panel.panelDisplay, YAHOO.dasBrowser.yuiContainer.panel[viewName]);
}

YAHOO.dasBrowser.genome.panel.panelDisplay = function(e, panel) {
	if(panel.cfg.getProperty("visible")){
		panel.hide();
		var eClientContainer = document.getElementById(panel.id + "Img");
		eClientContainer.innerHTML = "<img src='img/plus02.gif' class='panelImg'>";
		//alert(panel.id);
	} else {
		panel.show();
		var eClientContainer = document.getElementById(panel.id + "Img");
		eClientContainer.innerHTML = "<img src='img/minus02.gif' class='panelImg'>";
	}
} 


